<%@ page language="java" pageEncoding="UTF-8" %>
<%@ page import="com.ncr.teradata.bireport.*" %>
<%@ page import="java.util.Enumeration" %>
<%
	String viewid = "";
	String cityCode = InitParam.PROVINCECODE_IN_CITYCODE;
	String countryCode = InitParam.CITYCODE_IN_COUNTRYCODE;
	Enumeration e = request.getParameterNames();
	while (e.hasMoreElements())
	{
		String hdName = (String)e.nextElement();
		if (hdName.equals("viewid"))
			viewid = request.getParameter(hdName);
		if (hdName.equals("cityCode"))
			cityCode = request.getParameter(hdName);
		if (hdName.equals("countryCode"))
			countryCode = request.getParameter(hdName);
	}
	
	ReportServer rserver = ReportServer.getInstance();
	ReportViewMetadata rview = rserver.getReportViewMetadata(viewid);
	if (rview == null){
		out.write("您请求的视图不存在！");
		return;
	}
	Report report = new Report(rview.getReportName(),cityCode,countryCode);
	//add init filter
	if (rview.getFilters() != null && rview.getFilters().length > 0){
		String[][] filters = rview.getFilters();
		for (int i = 0; i < filters.length; i++){
			String[] filt = filters[i];
			String[] filtValue = new String[filt.length - 1];
			if (request.getParameter(filt[0]) != null)
				continue;
			for (int j = 1; j < filt.length; j++){
				if (filt[j].startsWith("$")){
					if(filt[j].substring(1).equalsIgnoreCase("DIMENSION_MAX_VALUE"))
						filtValue[j-1] = (String)report.getDimensionMaxValue(filt[0]);
					else if(filt[j].substring(1).equalsIgnoreCase("DIMENSION_MIN_VALUE"))
						filtValue[j-1] = (String)report.getDimensionMinValue(filt[0]);
					else if(filt[j].substring(1).equalsIgnoreCase("BETWEEN"))
						filtValue[j-1] = Report.BETWEEN;
					else if(filt[j].substring(1).equalsIgnoreCase("GREATER_THAN_OR_EQUAL"))
						filtValue[j-1] = Report.GREATER_THAN_OR_EQUAL;
					else if(filt[j].substring(1).equalsIgnoreCase("LESS_THAN_OR_EQUAL"))
						filtValue[j-1] = Report.LESS_THAN_OR_EQUAL;
					else
						filtValue[j-1] = filt[j];
				}
				else
					filtValue[j-1] = filt[j];
			}
			report.addFilter(filt[0],filtValue);
		}
	}
	//add paging filter
	StringBuffer pagingSelect = new StringBuffer();
	if (rview.getPagDimensions() != null && rview.getPagDimensions().length > 0){
		String[] pagDims = rview.getPagDimensions();
		for (int i = 0; i < pagDims.length; i++){
			String dimName = pagDims[i];
			if (dimName.startsWith("$")){
				if (dimName.substring(1).equalsIgnoreCase("DEFAULT_AREA_ID_ALIAS"))
					dimName = report.getDefaultAreaIdAlias();
				else if (dimName.substring(1).equalsIgnoreCase("DEFAULT_AREA_NAME_ALIAS"))
					dimName = report.getDefaultAreaNameAlias();
				else
					;
			}
			String filtValue = request.getParameter(dimName);
			if (filtValue != null && !filtValue.equals(""))
				report.addFilter(dimName,new String[]{filtValue});
			//html select control
			if (filtValue == null){
				if (rview.getFilters() != null && rview.getFilters().length > 0){
					String[][] initFilters = rview.getFilters();
					for (int j = 0; j < initFilters.length; j++){
						if (initFilters[j][0].equals(dimName)){
							if (initFilters[j][1].startsWith("$")){
								if(initFilters[j][1].substring(1).equalsIgnoreCase("DIMENSION_MAX_VALUE"))
									filtValue = (String)report.getDimensionMaxValue(dimName);
								else if(initFilters[j][1].substring(1).equalsIgnoreCase("DIMENSION_MIN_VALUE"))
									filtValue = (String)report.getDimensionMinValue(dimName);
								else
									filtValue = initFilters[j][1];
							}
							else
								filtValue = initFilters[j][1];
							break;
						}
					}
				}
			}
			filtValue = filtValue == null ? "" : filtValue;
			Object[] dimValues = null;
			if (dimName.equalsIgnoreCase(InitParam.DIMENSION_ALIAS_INSQL_COUNTRY) || dimName.equalsIgnoreCase(InitParam.DIMENSION_ALIAS_INSQL_COUNTRY_NAME))
				dimValues = report.getDimensionValues(dimName,InitParam.DIMENSION_ALIAS_INSQL_CITY,cityCode);
			else
				dimValues = report.getDimensionValues(dimName);
			pagingSelect.append("<td align='left' valign='top'>");
			pagingSelect.append(report.getColumnTitle(dimName));
			pagingSelect.append("<select class='select' name='" + dimName + "' style='width:80' onchange='submitForm();'>");
			pagingSelect.append("<option value='' " + (filtValue.equals("") ? "selected" : "") + ">全部</option>");
			if (dimName.equalsIgnoreCase(InitParam.DIMENSION_ALIAS_INSQL_DATE) || dimName.equalsIgnoreCase(InitParam.DIMENSION_ALIAS_INSQL_MONTH))
				for (int j = dimValues.length - 1; j >= 0; j--){
					pagingSelect.append("<option value='" + dimValues[j] + "' " + (filtValue.equals((String)dimValues[j]) ? "selected" : "") + ">" + dimValues[j] + "</option>");
				}
			else
				for (int j = 0; j < dimValues.length; j++){
					pagingSelect.append("<option value='" + dimValues[j] + "' " + (filtValue.equals((String)dimValues[j]) ? "selected" : "") + ">" + dimValues[j] + "</option>");
				}
			pagingSelect.append("</select></td>");
		}
	}
	//add col dimension
	if (rview.getColDimensions() != null && rview.getColDimensions().length > 0){
		String[] colDims = rview.getColDimensions();
		String[] colDimsName = new String[colDims.length];
		for (int i = 0; i < colDims.length; i++){
			if (colDims[i].startsWith("$")){
				if (colDims[i].substring(1).equalsIgnoreCase("DEFAULT_AREA_ID_ALIAS"))
					colDimsName[i] = report.getDefaultAreaIdAlias();
				else if (colDims[i].substring(1).equalsIgnoreCase("DEFAULT_AREA_NAME_ALIAS"))
					colDimsName[i] = report.getDefaultAreaNameAlias();
				else
					colDimsName[i] = colDims[i];
			}
			else
				colDimsName[i] = colDims[i];
		}
		report.addDimension(Report.COLUMN_DIMENSION,colDimsName);
	}
	//add row dimension
	if (rview.getRowDimensions() != null && rview.getRowDimensions().length > 0){
		String[] rowDims = rview.getRowDimensions();
		String[] rowDimsName = new String[rowDims.length];
		for (int i = 0; i < rowDims.length; i++){
			if (rowDims[i].startsWith("$")){
				if (rowDims[i].substring(1).equalsIgnoreCase("DEFAULT_AREA_ID_ALIAS"))
					rowDimsName[i] = report.getDefaultAreaIdAlias();
				else if (rowDims[i].substring(1).equalsIgnoreCase("DEFAULT_AREA_NAME_ALIAS"))
					rowDimsName[i] = report.getDefaultAreaNameAlias();
				else
					rowDimsName[i] = rowDims[i];
			}
			else
				rowDimsName[i] = rowDims[i];
		}
		report.addDimension(Report.ROW_DIMENSION,rowDimsName);
	}
	//add measure
	if (rview.getMeasures() != null && rview.getMeasures().length > 0){
		report.addMeasure(rview.getMeasures());
	}
	//add derive measure
	if (rview.getDeriveMeasures() != null && rview.getDeriveMeasures().length > 0){
		String[][] dmeasures = rview.getDeriveMeasures();
		for (int i = 0; i < dmeasures.length; i++){
			String[] dmea = dmeasures[i];
			String[] params = new String[dmea.length - 2];
			for (int j = 2; j < dmea.length; j++){
				params[j-2] = dmea[j];
			}
			String meth = dmea[1].startsWith("$") ? dmea[1].substring(1) : dmea[1];
			if (meth.equalsIgnoreCase("CYCLE_RATIO"))
				report.addDeriveMeasure(dmea[0],Report.CYCLE_RATIO,params);
			else if (meth.equalsIgnoreCase("COMPOSING_RATIO"))
				report.addDeriveMeasure(dmea[0],Report.COMPOSING_RATIO,params);
			else if (meth.equalsIgnoreCase("RANKING"))
				report.addDeriveMeasure(dmea[0],Report.RANKING,params);
			else if (meth.equalsIgnoreCase("RATIO"))
				report.addDeriveMeasure(dmea[0],Report.RATIO,params);
			else if (meth.equalsIgnoreCase("BRING_INCREASE_RATIO"))
				report.addDeriveMeasure(dmea[0],Report.BRING_INCREASE_RATIO,params);
			else if (meth.equalsIgnoreCase("INVERSE_RATIO"))
				report.addDeriveMeasure(dmea[0],Report.INVERSE_RATIO,params);
			else if (meth.equalsIgnoreCase("DIVIDE"))
				report.addDeriveMeasure(dmea[0],Report.DIVIDE,params);
			else if (meth.equalsIgnoreCase("DISCOUNT_RATIO"))
				report.addDeriveMeasure(dmea[0],Report.DISCOUNT_RATIO,params);
			else
				;
		}
	}
	//add subtotal
	if (rview.getSubtotals() != null && rview.getSubtotals().length > 0){
		String[] subtotals = rview.getSubtotals();
		for (int i = 0; i < subtotals.length; i++){
			if (subtotals[i].equalsIgnoreCase("BOTTOM"))
				report.addSubtotal(Report.BOTTOM);
			else if (subtotals[i].equalsIgnoreCase("TOP"))
				report.addSubtotal(Report.TOP);
			else if (subtotals[i].equalsIgnoreCase("LEFT"))
				report.addSubtotal(Report.LEFT);
			else if (subtotals[i].equalsIgnoreCase("RIGHT"))
				report.addSubtotal(Report.RIGHT);
			else
				;
		}
	}
%>

<html>
<head>
	<title><%= rview.getViewNameCN() %></title>
	<link href="<%= rview.getStyleFile() %>" rel="stylesheet" type="text/css"/>
	<link href="../css/style.css" rel="stylesheet" type="text/css"/>
    <link href="../css/font.css" rel="stylesheet" type="text/css"/>
<script>
	function submitForm(){
		bireportForm.submit();
	}
</script>
</head>
<body>
<form name="bireportForm" action="bireport.jsp" method="post">
	<table width="95%" height="100%"  border="0" align="center" cellpadding="1" cellspacing="1" >
		<tr>
			<td valign='top' align='left' bgcolor="#FFFFFF"> 
				<table border=0 width="100%" class="tablestyle4" style='table-layout:' cellpadding='2' cellspacing='2' bordercolordark='#ffffff' bordercolorlight='#ffffff'>
					<tr class="fontblue" width="100%" align="left">
						<td align='left' valign='center' title='<%= rview.getViewDesc() %>'>
							<img src="../images/title_left.gif" width="10" height="10">
                    		<%= rview.getViewNameCN() %>
                    	</td>
                    	<%= pagingSelect.toString() %>
                        <td align="right" valign='center'>
                        	<img src="../images/title_left.gif" width="10" height="10">
                        	<a href='<%= rview.getOlapUrl() %>' target='_blank' style='text-decoration:none;color:blue'>详情进入</a>
                        </td>
					</tr>
				</table>
				<%= report.buildTableData() %>
			</td>
		</tr>
	</table>
	<input type="hidden" name="viewid" value="<%= viewid %>" />
	<input type="hidden" name="cityCode" value="<%= cityCode %>" />
	<input type="hidden" name="countryCode" value="<%= countryCode %>" />
</form>
</body>
</html>